<?php
namespace Core\Widget;

use Model\Factory\TableGatewayFactory;

class WidgetTextBlock extends  WidgetAbstract{

    public function form()
    {
        $html = '<div id="widgetContainer"><table cellspacing="5px" cellpadding="5px">
        <tr>
        <td>Tên block(*): </td>
        <td><input id="txtBlockName" name="txtBlockName" class="k-textbox" style="width: 250px" required placeholder="Nhập tên, bắt buộc" validationMessage="Chưa nhập tên">
            <span class="k-invalid-msg" data-for="txtBlockName"></span>
        </td>
        </tr>
        <tr>
        <td>Tiêu đề(*): </td>
        <td><input id="txtHeader" name="txtText" class="k-textbox" style="width: 350px" required placeholder="Nhập tiêu đề muốn hiển thị, bắt buộc" validationMessage="Chưa nhập tiêu dề">
            <span class="k-invalid-msg" data-for="txtHeader"></span>
        </td>
        </tr>
        <tr>
        <td>Text(*): </td>
        <td><textarea id="txtText" name="txtText" class="k-textbox" style="width: 350px" required placeholder="Nhập text muốn hiển thị, bắt buộc" validationMessage="Chưa nhập text"></textarea>
            <span class="k-invalid-msg" data-for="txtText"></span>
        </td>
        </tr>
        </table>
        </div>';
        $programCode = $this->getProgramCode();
        $javascript = '<script type="text/javascript">
            widgetValidator = $("#widgetContainer").kendoValidator().data("kendoValidator");
            function validateWidgetForm(){
                 return widgetValidator.validate();
            }
            function doAddWidget(){
                if(!validateWidgetForm()){
                    return;
                }
                blockName = $("#txtBlockName").val();
                txtText = $("#txtText").val();
                txtHeader = $("#txtHeader").val();
                widgetName = "'.$programCode.'";
                resultArr = new Array("blockName");
                resultArr["blockName"] = blockName;
                resultArr["text"] = txtText;
                resultArr["header"] = txtHeader;
                resultArr["widgetName"] = widgetName;

                return {name:blockName, text:txtText, header:txtHeader,  widgetName:"'.$programCode.'"};

            }
        </script>';
        return $html.$javascript;
    }

    public function render($blockInstanceItem)
    {

        $params = $blockInstanceItem->params;
        $paramArr = $this->getArrayFromJson($params);
        $text = $paramArr['text'];
        $header = $paramArr['header'];
        $html = '<h3>'.$header.'</h3><p>'.$text.'</p>';
        return $html;
    }

    public function save($allParams)
    {
        $text = $allParams['text'];
        $header = $allParams['header'];
        $name = $allParams['name'];
        $widgetName = $allParams['widgetName'];

        $params = $this->buildJSONString(array('text'=>$text, 'header'=>$header));
        $blockClass = TableGatewayFactory::getBlockClassTable();

        $blockClass_Item = $blockClass->findByProgramCode($widgetName);
        $blockInstance = TableGatewayFactory::getBlockInstanceTable();

        if($allParams['widgetId']){
            $blockInstance->update(array('block_class_id'=>$blockClass_Item->id,
                'name'=>$name, 'params'=>$params,
                ),
                $allParams['widgetId']);
            return $allParams['widgetId'] ;
        }

        else{
            $id = $blockInstance->insert(array(
                'block_class_id'=>$blockClass_Item->id,
                'name'=>$name, 'params'=>$params,));
            return $id;
        }


    }

    public function form_update($blockInstance_Item, $blockClass_Item)
    {

        $paramArr = $this->getArrayFromJson($blockInstance_Item->params);
        $html = '<h3>'.$blockClass_Item->name.'</h3>
        <input id="txtWidgetId" type="hidden" value="'.$blockInstance_Item->id.'">
        <div id="widgetContainer"><table cellspacing="5px" cellpadding="5px">
        <tr>
        <td>Tên block(*): </td>
        <td><input value="'.$blockInstance_Item->name.'" id="txtBlockName" name="txtBlockName" class="k-textbox" style="width: 250px" required placeholder="Nhập tên, bắt buộc" validationMessage="Chưa nhập tên">
            <span class="k-invalid-msg" data-for="txtBlockName"></span>
        </td>
        </tr>
        <tr>
        <td>Tiêu đề(*): </td>
        <td><input value="'.$paramArr['header'].'" id="txtHeader" name="txtHeader" class="k-textbox" style="width: 350px" required placeholder="Nhập tiêu đề muốn hiển thị, bắt buộc" validationMessage="Chưa nhập tiêu đề">
            <span class="k-invalid-msg" data-for="txtHeader"></span>
        </td>
        </tr>
        <tr>
        <td>Text(*): </td>
        <td><textarea  id="txtText" name="txtText" class="k-textbox" style="width: 350px" required placeholder="Nhập text muốn hiển thị, bắt buộc" validationMessage="Chưa nhập text">'.$paramArr['text'].'</textarea>
            <span class="k-invalid-msg" data-for="txtText"></span>
        </td>
        </tr>
        </table>
        </div>';
        $javascript = '<script type="text/javascript">
            widgetValidator = $("#widgetContainer").kendoValidator().data("kendoValidator");
            function validateWidgetForm(){
                 return widgetValidator.validate();
            }
            function doUpdateWidget(){
                if(!validateWidgetForm()){
                    return;
                }
                blockName = $("#txtBlockName").val();
                txtText = $("#txtText").val();
                txtHeader = $("#txtHeader").val();
                widgetName = "'.$this->getProgramCode().'";
                txtWidgetId = $("#txtWidgetId").val();


                return {name:blockName, text:txtText, header:txtHeader,
                widgetName:"'.$this->getProgramCode().'",widgetId:txtWidgetId};

            }
        </script>';
        return $html.$javascript;
    }

    public function preview($allParams)
    {

        $text = $allParams['text'];
        $header = $allParams['header'];
        $html = '<h3>'.$header.'</h3><p>'.$text.'</p>';
        return $html;
    }

    public function form_content($blockInstance_Item)
    {
        $params = $blockInstance_Item->params;
        $paramArr = $this->getArrayFromJson($params);
        $text = $paramArr['text'];
        $header = $paramArr['header'];
        $html = '<input style="width:90%;" value="'.$header.'" id="txtContentHeader_'.$blockInstance_Item->id.'">
        <br><textarea style="width:90%;" id="txtContentText_'.$blockInstance_Item->id.'">'.$text.'</textarea>';
        $javascript = '<script type="text/javascript">

            function validateWidgetForm(){
                return true;
            }
            function doUpdateWidgetContent(divId){
                if(!validateWidgetForm()){
                    return;
                }

                txtText = $("#txtContentText_'.$blockInstance_Item->id.'").val();
                txtHeader = $("#txtContentHeader_'.$blockInstance_Item->id.'").val();


        $.post("/admin/block/save-block-content",{widgetName:"'.$this->getProgramCode().'", header:txtHeader, text:txtText, widgetId:'.$blockInstance_Item->id.'}
            ,function(result){
                cancelBlockEdit(divId);
                $("#"+divId).html("<h3>"+txtHeader+"</h3><p>"+txtText+"</p>");
            });
            }
        </script>';
        return $html.$javascript;
    }

    public function saveContent($allParams)
    {
        $text = $allParams['text'];
        $header = $allParams['header'];
        $widgetName = $allParams['widgetName'];
        $widgetId = $allParams['widgetId'];


        $params = $this->buildJSONString(array('text'=>$text, 'header'=>$header));
        $blockClass = TableGatewayFactory::getBlockClassTable();

        $blockInstanceTable = TableGatewayFactory::getBlockInstanceTable();
        $blockInstance = $blockInstanceTable->getBlockInstance($widgetId);

        $blockClass_Item = $blockClass->findByProgramCode($widgetName);

        $blockInstanceTable->update(array('block_class_id'=>$blockClass_Item->id,
                'name'=>$blockInstance->name,
                 'params'=>$params),
            $allParams['widgetId']);
        return $allParams['widgetId'] ;
    }


    public function getProgramCode()
    {
        return "TextBlock";
    }
}
